package com.eee.xiang.market.service.impl;

import com.eee.xiang.market.beans.OperateResponse;
import com.eee.xiang.market.beans.TCargo;
import com.eee.xiang.market.beans.TCargoExample;
import com.eee.xiang.market.beans.TUser;
import com.eee.xiang.market.common.TableData;
import com.eee.xiang.market.dao.mapper.TCargoMapper;
import com.eee.xiang.market.service.ICargoService;
import com.eee.xiang.market.utils.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class CargoServiceImpl implements ICargoService {

    private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Value("${file.uploadFolder}")
    private String uploadFolder;

    @Autowired
    private TCargoMapper cargoMapper;

    @Override
    public TableData getAllCargoList(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        TCargoExample example = new TCargoExample();
        example.createCriteria().andDeleteFlagEqualTo((short) 0);
        List<TCargo> tCargos = cargoMapper.selectByExample(example);
        return new TableData(tCargos.size(), tCargos);
    }

    @Override
    public OperateResponse uploadImg(MultipartFile imgFile) {
        String originalFilename = imgFile.getOriginalFilename();
        File uploadFile = new File(uploadFolder+originalFilename);
        if(!uploadFile.getParentFile().exists()){
            uploadFile.getParentFile().mkdirs();
        }
        try {
            imgFile.transferTo(uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return OperateResponse.obtainSuccessResponse("/upload/" + originalFilename);
    }

    @Override
    public OperateResponse saveCargo(TCargo cargoForm) {
        if(!Strings.isBlank(cargoForm.getCargoId())){
            int i = cargoMapper.updateByPrimaryKeySelective(cargoForm);
            if(i > 1){
                return OperateResponse.obtainSuccessResponse(true);
            }else{
                return OperateResponse.failResponse("更新商品信息失败！");
            }
        }
        String cargoId = UUIDUtil.generate();
        cargoForm.setCargoId(cargoId);
        cargoForm.setCreateDatetime(new Date());
        int i = cargoMapper.insertSelective(cargoForm);
        if(i > 0){
            return OperateResponse.obtainSuccessResponse(true);
        }else{
            return OperateResponse.failResponse("保存商品信息失败！");
        }
    }

    @Override
    public OperateResponse getDetailCargo() {
        List<TCargo> tCargos = cargoMapper.selectByExample(null);
        return OperateResponse.obtainSuccessResponse(tCargos);
    }

    @Override
    public OperateResponse delCargo(String cargoId) {
        return OperateResponse.obtainSuccessResponse(cargoMapper.delCargo(cargoId));
    }
}
